package Service.Teacher.Imp;

import Dao.DaoFactory;
import Dao.MsgAndEmail.EmailDao;
import Dao.MsgAndEmail.MessageDao;
import Dao.Role.AdminDao;
import Dao.Role.StuInfoDao;
import Dao.Role.TeacherDao;
import Entity.MsgAndEmail.Email;
import Entity.MsgAndEmail.Message;
import Entity.MyTableData;
import Entity.Role.Admin;
import Entity.Role.Student;
import Entity.Role.Teacher;
import Service.Teacher.MsgAndEmailService;
import Util.DBUtil;
import Util.EmailUtil;

import javax.mail.MessagingException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class MsgAndEmailServiceImp implements MsgAndEmailService {
    private MessageDao messageDao;
    private EmailDao emailDao;
    private StuInfoDao stuInfoDao;
    private TeacherDao teacherDao;
    private AdminDao adminDao;
    private Connection conn;

    public MsgAndEmailServiceImp() {
        emailDao = DaoFactory.getInstance("emailDao");
        messageDao = DaoFactory.getInstance("messageDao");
        stuInfoDao = DaoFactory.getInstance("stuInfoDao");
        teacherDao = DaoFactory.getInstance("teacherDao");
        adminDao = DaoFactory.getInstance("adminDao");
        conn = DBUtil.getConnByDruid();
    }

    @Override
    public MyTableData getAllMessage(String queryKey, Integer offset, Integer limit) {
        List<Message> messages = messageDao.getAllMessage(queryKey, offset, limit, conn);
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", messages.size(), messages);
    }

    @Override
    public MyTableData getAllEmail(String queryKey, Integer offset, Integer limit, String NO, Integer type) {
        List<Email> emails = emailDao.getAllEmail(queryKey, offset, limit, NO, type, conn);
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", emails.size(), emails);
    }


    @Override
    public Integer writeEmail(Email email, Integer role) {
        // 1001 发送成功 ， 1002 要发送的对象不存在 . 1003 发送失败/邮箱授权码错误 1004 邮箱授权码未设置
        int code = 1003;
        String emailCode;
        // 查询要发送的对象是否存在 role 1是学生  2是教师 3是管理员
        Admin admin = new Admin();
        Student student = new Student();
        Teacher teacher = new Teacher();
        List<?> receive = new ArrayList<>();
        String receiveNO = email.getReceiveNo();
        if (role == 1) {
            receive = stuInfoDao.getStuByNO(receiveNO, conn);
        } else if (role == 2) {
            receive = teacherDao.getTeacherByNO(receiveNO, conn);
        } else if (role == 3) {
            receive = adminDao.getAdminByName(receiveNO, conn);
        }
        if (receive.size() == 0) {
            code = 1002;
        } else {
            //设置收件人的信息  要发送的对象存在 ：
            if (role == 1) {
                student = (Student) receive.get(0);
                email.setReceiveEmail(student.getEmail());
                email.setReceiveName(student.getName());
            } else if (role == 2) {
                teacher = (Teacher) receive.get(0);
                email.setReceiveEmail(teacher.getEmail());
                email.setReceiveName(teacher.getName());
            } else {
                admin = (Admin) receive.get(0);
                email.setReceiveEmail(admin.getAdminEmail());
                email.setReceiveEmail(admin.getRealName());
            }
            //获取发送人的邮箱和真实姓名 和 邮箱的授权码
            String sendNO = email.getSendNo();
            receive = stuInfoDao.getStuByNO(sendNO, conn);
            if (receive.size() == 0) {
                receive = teacherDao.getTeacherByNO(sendNO, conn);
                if (receive.size() == 0) {
                    receive = adminDao.getAdminByName(sendNO, conn);
                    admin = (Admin) receive.get(0);
                    email.setSendName(admin.getRealName());
                    email.setSendEmail(admin.getAdminEmail());
                    emailCode = admin.getEmailCode();
                } else {
                    teacher = (Teacher) receive.get(0);
                    email.setSendName(teacher.getName());
                    email.setSendEmail(teacher.getEmail());
                    emailCode = teacher.getEmailCode();
                }
            } else {
                student = (Student) receive.get(0);
                email.setSendName(student.getName());
                email.setSendEmail(student.getEmail());
                emailCode = student.getEmailCode();
            }

            //判断邮箱的授权码
            if (emailCode == null || "".equals(emailCode) || emailCode.length() != 16) {
                code = 1004;
            } else {
                //调用第三方API
                boolean flag = EmailUtil.sendEmail(email, emailCode);
                //发送成功
                if (flag) {
                    //往数据库插入一条记录
                    if (emailDao.insertEmail(email, conn) == 1) {
                        code = 1001;
                    }
                }
            }
        }
        DBUtil.closeConn(conn);
        return code;
    }

    @Override
    public Boolean updateStateById(Integer id) {
        boolean flag = emailDao.updateStateById(id, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }


    @Override
    public Boolean updateUserEmail(String no, String newEmail, String role) {
        boolean flag = false;
        if ("1".equals(role)) {
            flag = stuInfoDao.updateEmailByNO(no, newEmail, conn) == 1;
        } else if ("2".equals(role)) {
            flag = teacherDao.updateEmailByNO(no, newEmail, conn) == 1;
        } else if ("3".equals(role)) {
            flag = adminDao.updateEmailByNO(no, newEmail, conn) == 1;
        }
        DBUtil.closeConn(conn);
        return flag;
    }

    @Override
    public Boolean updateEmailCode(String no, String emailCode, String role) {
        boolean flag = false;
        if ("1".equals(role)) {
            flag = stuInfoDao.updateEmailCodeByNO(no, emailCode, conn) == 1;
        } else if ("2".equals(role)) {
            flag = teacherDao.updateEmailCodeByNO(no, emailCode, conn) == 1;
        } else if ("3".equals(role)) {
            flag = adminDao.updateEmailCodeByNO(no, emailCode, conn) == 1;
        }
        DBUtil.closeConn(conn);
        return flag;
    }
}
